/*
 *    This file is part of BlobDB.
 *
 *    BlobDB is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    BlobDB is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with BlobDB.  If not, see <http://www.gnu.org/licenses/>.
 *    
 *    Copyright(c) 2011 Michael Kankkonen
 *    http://www.vnetcon.org
 */


package org.vnetcon.blobdb.database.util.parser;

import java.util.ArrayList;

import org.vnetcon.blobdb.database.SQLException;
import org.vnetcon.blobdb.database.util.ColumnInfo;

public class InsertTableParser {

	private ArrayList<String> elements = null;

	private String table = null;
	private ColumnInfo colInfo = null;

	public InsertTableParser(ArrayList<String> elements) throws SQLException{
		this.elements = elements;
	}
	
	
	public String getTable(){
		return this.table;
	}

	public void setColumnInfo(ColumnInfo colInfo){
		this.colInfo = colInfo;
	}
	
	public ColumnInfo getColumnInfo(){
		return this.colInfo;
	}
	
	public void initTable() throws SQLException {
		int i = 0;
		String buf = null;
		table = elements.get(2);
		buf = elements.get(3);
		if(!buf.equals("(") && !buf.toUpperCase().equals("VALUES")){
			throw new SQLException("Expecting '(' or 'VALUES' after insert tablename.");
		}
	}

	public void initColumns() throws SQLException {
		int i = 0;
		ArrayList<String> cols = new ArrayList<String>();
		ArrayList<String> values = new ArrayList<String>();
		String buf = null;
		table = elements.get(2);
		buf = elements.get(3);
		if(!buf.equals("(") && !buf.toUpperCase().equals("VALUES")){
			throw new SQLException("Expecting '(' or 'VALUES' after insert tablename.");
		}
		i = 4;
		if(buf.equals("(")){
			while(true){
				buf = elements.get(i);
				if(buf.equals(",")){
					i++;
					continue;
				}
				if(buf.equals(")")){
					break;
				}
//				System.out.println("ADDING INSERT COL: " + buf);
				cols.add(buf);
				i++;
			}
		}else{
			throw new SQLException("Expecting '(' after table name. Found: " + buf);
		}
		
		i++;
		buf = elements.get(i);
		if(buf.toUpperCase().equals("VALUES")){
			i++;
			buf = elements.get(i);
			if(buf.equals("(")){
				i++;
				while(true){
					buf = elements.get(i);
					if(buf.equals(",")){
						i++;
						continue;
					}
					if(buf.equals(")")){
						break;
					}
//					System.out.println("ADDING VALUES COL: " + buf);
					values.add(buf);
					i++;
				}
			}
		}else{
			throw new SQLException("Expecting 'VALUES' after column list. Found: " + buf);
		}
		i++;
		if(i == elements.size()){
			throw new SQLException("Expecting ';' at the end of statement.");
		}
		buf = elements.get(i);
		if(!buf.equals(";")){
			throw new SQLException("Expecting ';' at the end of statement.");
		}
		
		if(cols.size() != values.size()){
			throw new SQLException("Column count is different from values count: " + cols.size() + "/" + values.size());
		}

		i = 0;
		while(i < cols.size()){
			String colName = cols.get(i);
			String colValue = values.get(i);
			colInfo.setColValue(colName, colValue);
			i++;
		}
		
//		System.out.println(cols);
	}
	
}
